package per.mnxc.excel.tree.util;

import cn.hutool.core.util.ReflectUtil;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author jiangao
 * @since 2025/1/20
 */


public class ReflectUtils extends ReflectUtil {

    public static <T> Object invokeGetter(T t, Field field) {
        final Method getterMethod = getGetterMethod(t, field);
        if (getterMethod == null) {
            return null;
        }
        return invoke(t, getterMethod);
    }
    public static <T> Method getGetterMethod(T t, Field field) {
        String fieldName = field != null ? field.getName() : "";
        if(StringUtils.isBlank(fieldName) || t == null){
            return null;
        }
        String setterMethodName = "get" + StringUtils.capitalize(fieldName);
        return getMethodByName(t.getClass(), setterMethodName);
    }
    public static <T> Method getSetterMethod(T t, Field field) {
        String fieldName = field != null ? field.getName() : "";
        if(StringUtils.isBlank(fieldName) || t == null){
            return null;
        }
        String setterMethodName = "set" + StringUtils.capitalize(fieldName);
        return getMethodByName(t.getClass(), setterMethodName);
    }
    public static <T> Object invokeSetter(T t, Field field, Object value) {
        final Method setterMethod = getSetterMethod(t, field);
        if (setterMethod == null) {
            return null;
        }
        return invoke(t, setterMethod, value);
    }
}
